﻿
//Author: 张亚涛
//createtime:2016-02-28 14:28:42
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NS.DAL.Systems;
using Model;
using DAL;

namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "MemberPay" 表的方法;
    /// </summary>
    public class MemberPayBase
    {
        const string SQL_Select = "select [ID],[OpenID],[Money],[TradeNo],[Date] from [MemberPay] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回MemberPay表的所有数据 
        /// </summary>
        /// <returns></returns>        
        public static List<MemberPay> GetAllMemberPay()
        {
            List<MemberPay> memberPayList = new List<MemberPay>();
            SqlDataReader reader = null;

            string sql = SQL_Select;
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                MemberPay memberPay = new MemberPay(reader);
                memberPayList.Add(memberPay);
            }
            reader.Close();
            return memberPayList;
        }
        /// <summary>
        /// 根据MemberPay表的ID字段返回数据 
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>       
        public static MemberPay GetMemberPayById(long id)
        {
            MemberPay memberPay = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                memberPay = new MemberPay(reader);
            }
            reader.Close();
            return memberPay;
        }
        /// <summary>
        /// 判断交易单号是否已存在
        /// </summary>
        /// <param name="TradeNo">交易单号</param>
        /// <returns>True表示存在，Flase表示不存在</returns>
        public static bool IsExistTradeNo(string tradeNo)
        {
            string SQL_Select = @"select count(*) from MemberPay where TradeNo=@TradeNo";
            SqlParameter[] parameters ={
                                         new SqlParameter("@TradeNo",tradeNo)
                                       };
            string ResultStr = SQLHelper.ExecuteScalar(SQL_Select, parameters);
            if (!string.IsNullOrEmpty(ResultStr) && int.Parse(ResultStr) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获得返利明细
        /// </summary>
        /// <param name="month">月份</param>
        /// <returns></returns>
        public static DataTable GetRebateDetail(string month)
        {
            decimal resDecimal;

            decimal bl1 = decimal.TryParse(DictionaryBase.GetDicValue("返利比例", "一级"), out resDecimal) ? resDecimal : 0;
            decimal bl2 = decimal.TryParse(DictionaryBase.GetDicValue("返利比例", "二级"), out resDecimal) ? resDecimal : 0;
            decimal bl3 = decimal.TryParse(DictionaryBase.GetDicValue("返利比例", "三级"), out resDecimal) ? resDecimal : 0;

            string strSQL = "SELECT " +
                            "	a.OpenID, " +
                            "	a.Mobile, " +
                            "	a.TrueName, " +
                            "	dbo.f_decimal2(a.mCount) mCount " +
                            "FROM (SELECT " +
                            "	m.OpenID, " +
                            "	m.Mobile, " +
                            "	m.TrueName, " +
                            "	(SELECT " +
                            "		ISNULL(SUM([Money]), 0) * " + bl1 + " AS pCount " +
                            "	FROM MemberPay " +
                            "	WHERE OpenID IN (SELECT " +
                            "		MemberID " +
                            "	FROM [MemberRelation] " +
                            "	WHERE (PartnerID = m.OpenID)) " +
                            "	AND CONVERT(varchar(7), Date, 120) = '" + month + "') " +
                            "	+ (SELECT " +
                            "		ISNULL(SUM([Money]), 0) * " + bl2 + " AS pCount " +
                            "	FROM MemberPay " +
                            "	WHERE OpenID IN (SELECT " +
                            "		MemberID " +
                            "	FROM [MemberRelation] " +
                            "	WHERE (PartnerID2 = m.OpenID)) " +
                            "	AND CONVERT(varchar(7), Date, 120) = '" + month + "') " +
                            "	+ (SELECT " +
                            "		ISNULL(SUM([Money]), 0) * " + bl3 + " AS pCount " +
                            "	FROM MemberPay " +
                            "	WHERE OpenID IN (SELECT " +
                            "		MemberID " +
                            "	FROM [MemberRelation] " +
                            "	WHERE (PartnerID3 = m.OpenID)) " +
                            "	AND CONVERT(varchar(7), Date, 120) = '" + month + "') " +
                            "	mCount " +
                            "FROM Member m) a " +
                            "WHERE a.mCount > 0 ";

            return SQLHelper.ExecuteDataSet(strSQL, null).Tables[0];
        }
        /// <summary>
        /// 获得返利余额
        /// </summary>
        /// <param name="openID">微信号</param>
        /// <returns></returns>
        public static DataTable GetRebateBlance(string openID)
        {
            decimal resDecimal;

            decimal bl1 = decimal.TryParse(DictionaryBase.GetDicValue("返利比例", "一级"), out resDecimal) ? resDecimal : 0;
            decimal bl2 = decimal.TryParse(DictionaryBase.GetDicValue("返利比例", "二级"), out resDecimal) ? resDecimal : 0;
            decimal bl3 = decimal.TryParse(DictionaryBase.GetDicValue("返利比例", "三级"), out resDecimal) ? resDecimal : 0;

            string strSQL = "SELECT " +
                            "	c.OpenID, " +
                            "	dbo.f_decimal2(c.mCount) mCount, " +
                            "	dbo.f_decimal2(c.bCount) bCount, " +
                            "	dbo.f_decimal2(c.mCount - c.bCount) Blance " +
                            "FROM (SELECT " +
                            "	a.OpenID, " +
                            "	ISNULL(a.mCount, 0) mCount, " +
                            "	ISNULL(b.bCount, 0) bCount " +
                            "FROM (SELECT " +
                            "	m.OpenID, " +
                            "	m.Mobile, " +
                            "	m.TrueName, " +
                            "	(SELECT " +
                            "		ISNULL(SUM([Money]), 0) * " + bl1 + " AS pCount " +
                            "	FROM MemberPay " +
                            "	WHERE OpenID IN (SELECT " +
                            "		MemberID " +
                            "	FROM [MemberRelation] " +
                            "	WHERE (PartnerID = m.OpenID))) " +
                            "	+ (SELECT " +
                            "		ISNULL(SUM([Money]), 0) * " + bl2 + " AS pCount " +
                            "	FROM MemberPay " +
                            "	WHERE OpenID IN (SELECT " +
                            "		MemberID " +
                            "	FROM [MemberRelation] " +
                            "	WHERE (PartnerID2 = m.OpenID))) " +
                            "	+ (SELECT " +
                            "		ISNULL(SUM([Money]), 0) * " + bl3 + " AS pCount " +
                            "	FROM MemberPay " +
                            "	WHERE OpenID IN (SELECT " +
                            "		MemberID " +
                            "	FROM [MemberRelation] " +
                            "	WHERE (PartnerID3 = m.OpenID))) " +
                            "	mCount " +
                            "FROM Member m) a " +
                            "LEFT JOIN (SELECT " +
                            "	OpenID, " +
                            "	SUM(Money) AS bCount " +
                            "FROM MemberAccount " +
                            "WHERE OpType = '返利' " +
                            "GROUP BY OpenID) b " +
                            "	ON a.OpenID = b.OpenID) c " +
                            "WHERE c.OpenID = '" + openID + "' ";

            return SQLHelper.ExecuteDataSet(strSQL, null).Tables[0];
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据MemberPay表的ID字段删除数据 
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>        
        public static int DeleteMemberPayById(long id)
        {
            string sql = "delete from [MemberPay] where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(MemberPay memberPay)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",memberPay.Id),
            new SqlParameter("@OpenID",memberPay.OpenID),
            new SqlParameter("@Money",memberPay.Money),
            new SqlParameter("@TradeNo",memberPay.TradeNo),
            new SqlParameter("@Date",memberPay.Date)    
            };
            return paras;
        }
        /// <summary>
        /// 根据MemberPay表的ID字段更新数据 
        /// </summary> 
        /// <param name="MemberPay">memberPay</param>
        /// <returns></returns>       
        public static int UpdateMemberPayById(MemberPay memberPay)
        {
            string sql = "update [MemberPay] set [OpenID] = @OpenID,[Money] = @Money,[TradeNo] = @TradeNo,[Date] = @Date where [ID] = @Id";
            SqlParameter[] paras = PrepareCommandParameters(memberPay);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向MemberPay表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="memberPay">MemberPay</param>       
        /// <returns></returns>        
        public static int AddMemberPay(MemberPay memberPay)
        {
            string sql = "insert into [MemberPay]([OpenID],[Money],[TradeNo],[Date])values(@OpenID,@Money,@TradeNo,@Date)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(memberPay);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 返回MemberPay表的所有数据 （SQL语句）
        /// </summary>
        /// <returns></returns>        
        public static string GetAllMemberPay_SQL()
        {
            return SQL_Select;
        }
        #endregion
    }
}